package com.wsoft.form.manager;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.dto.WfFormTableDto;
import com.wsoft.form.entity.WfFormColumnEntity;
import com.wsoft.form.entity.WfFormTableEntity;
import com.wsoft.form.entity.WfFormTableRelationEntity;
import com.wsoft.form.mapper.WfFormTableMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/29 13:51
 */
@Service
public class WfFormTableManager extends ServiceImpl<WfFormTableMapper, WfFormTableEntity> {

    @Resource
    private RedisService redisService;
    @Resource
    private WfFormTableRelationManager relationManager;
    @Resource
    private WfFormColumnManager columnManager;

    /**
     * 获取table信息
     *
     * @param tableId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.TABLE_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormTableEntity getById(Long tableId) {
        return  super.getById(tableId);
    }


    /**
     * 返回table信息 - 带relation 信息
     * @param tableId
     * @return
     */
    public WfFormTableDto getTableInfo(Long tableId) {
        WfFormTableEntity tableEntity = getById(tableId);
        WfFormTableDto convert = Convert.convert(WfFormTableDto.class, tableEntity);

        //查出关联类型 和 关联字段
        List<WfFormTableRelationEntity> relationList = relationManager.getByFormId(tableEntity.getFormId());
        WfFormTableRelationEntity relationEntity = relationList.stream().filter(item -> item.getTableId().equals(tableId)).findAny().get();
        convert.setRelationType(relationEntity.getRelationType());
        if(null != relationEntity.getSlaveColumnId()){
            WfFormColumnEntity columnEntity = columnManager.getById(relationEntity.getSlaveColumnId());
            convert.setRelationColumn(columnEntity.getColumnName());
        }
        return convert;
    }


    /**
     * 根据tableId 集合查询
     *
     * @param ids
     * @return
     */
    @Override
    public List<WfFormTableEntity> listByIds(Collection<? extends Serializable> ids) {
        List<WfFormTableEntity> list = new ArrayList<>();
        for (Serializable id : ids) {
            list.add(this.getById(id));
        }
        return list;
    }

    /**
     * 根据 tableName 查询
     *
     * @param tableName 表名
     * @return WfFormTableEntity
     */
    public List<WfFormTableEntity> getByTableName(String tableName) {
        if (StrUtil.isBlank(tableName)) {
            return new ArrayList<>();
        }
        QueryWrapper<WfFormTableEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableEntity::getTableName, tableName);
        return list(query);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFormTableEntity entity) {
        boolean b = super.save(entity);
        if(b){
            redisService.del(RedisKeyConstant.Form.TABLE_LIST);
        }
        return b;
    }

    /**
     * 跟新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFormTableEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.TABLE_INFO  + entity.getId());
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 删除
     *
     * @param tableId
     * @return
     */
    public boolean removeById(Long tableId) {
        boolean b = super.removeById(tableId);
        if (b) {
            redisService.del(RedisKeyConstant.Form.TABLE_INFO + tableId);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }


    /**
     * 根据 formId 删除
     *
     * @param formId formId
     * @return boolean
     */
    public boolean removeByFormId(Long formId) {
        QueryWrapper<WfFormTableEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableEntity::getFormId, formId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return remove(query);
    }

    /**
     * 查询formId下 表列表
     *
     * @param formId formId
     * @return list
     */
    @GetRedisCache(type = RedisKeyConstant.Form.TABLE_LIST,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<WfFormTableEntity> getByFormId(Long formId) {
        QueryWrapper<WfFormTableEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableEntity::getFormId, formId);
        return list(query);
    }

}
